import java.util.ArrayList;

public class Tache implements Comparable<Tache>{

	private static int nbTache = 0;
	private int numTache;

	private String libelle;
	private float duree;
	private char code;
	private ArrayList<Tache> suivant;
	private ArrayList<Tache> precedent;

	private float dateDebutPlusTot;
	private float dateDebutPlusTard;

	public Tache(char code, float duree, String libelle) {
		assert (code >= 65 && code <= 90);
		assert (duree >= 0);
		assert (libelle != null);

		this.numTache = ++nbTache;

		this.code = code;
		this.duree = duree;
		this.libelle = libelle;

		this.suivant = new ArrayList<Tache>();
		this.precedent = new ArrayList<Tache>();
	}

	public void ajTachePrec(Tache tPrec) {
		assert (tPrec != null);

		if (!this.precedent.contains(tPrec)) {
			this.precedent.add(tPrec);
			tPrec.suivant.add(this);
		}
	}

	public String toString() {
		String s;

		s = String.format("%1$-5s : %5$-30s [ %2$6.2f; %3$6.2f] --- %4$5.2f --> ", this.code, this.dateDebutPlusTot, this.dateDebutPlusTard,
				this.duree, this.libelle);

		if (this.suivant.isEmpty()) {
			return s += "-";
		}

		for (Tache t : this.suivant) {
			s += t.code + " ";
		}

		return s;
	}

	public boolean equals(Tache t) {
		return (this.numTache == t.numTache);
	}

	public void supprTacheSuiv(Tache tSuiv) {
		assert (tSuiv != null);

		for (int i = 0; i < this.suivant.size(); i++) {
			Tache tCour = this.suivant.get(i);
			if (tCour.equals(tSuiv)) {
				this.suivant.remove(tSuiv);
			}
		}
	}

	public void supprTachePrec(Tache tPrec) {
		assert (tPrec != null);

		for (int i = 0; i < this.precedent.size(); i++) {
			Tache tCour = this.precedent.get(i);
			if (tCour.equals(tPrec)) {
				this.precedent.remove(tPrec);
			}
		}
	}
	
	public int compareTo ( Tache t ) {
		return this.code - t.code;
	}

	public float getDuree() {
		return this.duree;
	}

	public char getCode() {
		return this.code;
	}

	public int getNumTache() {
		return this.numTache;
	}

	public ArrayList<Tache> getPrec() {
		return this.precedent;
	}

	public ArrayList<Tache> getSuiv() {
		return this.suivant;
	}

	public float getDateDebutPlusTard() {
		return this.dateDebutPlusTard;
	}

	public float getDateDebutPlusTot() {
		return this.dateDebutPlusTot;
	}

	public void setDateDebutPlusTard(float dateDebutPlusTard) {
		assert (dateDebutPlusTard >= 0);
		this.dateDebutPlusTard = dateDebutPlusTard;
	}

	public void setDateDebutPlusTot(float dateDebutPlusTot) {
		assert (dateDebutPlusTot >= 0);
		this.dateDebutPlusTot = dateDebutPlusTot;
	}
}
